﻿
#include "conn_mgr.h"

#include <conf_mgr.h>

#define TICK_TIME 100 // tick触发时间（毫秒）

CConnMgr *g_objConnMgr = CConnMgr::ins();
//////////////////////////////////////////////////////////////////

CConnMgr::CConnMgr() {
    mPacket         = SDHWPacketBuffer::instance();
    mUartMCU        = 0;
    mNextEventTime  = 0;
    mLastStatusTime = 0;
}

CConnMgr::~CConnMgr() {
    __del(mUartMCU);
}

int CConnMgr::init() {
    UartOpenReq ss;

    snprintf(ss.serialPort, sizeof(ss.serialPort), "/dev/ttyS2");
    ss.speed     = 9600;
    ss.flow_ctrl = 0;
    ss.databits  = 8;
    ss.stopbits  = 1;
    ss.parity    = 'N';

#if 0
    mUartMCU = new UartClient(mPacket, BT_PHYSICS_BTN, ss, "192.168.3.41", 9000, 60);
    mUartMCU->init();
#endif

    // 启动延迟一会后开始发包
    mNextEventTime = SystemClock::uptimeMillis() + TICK_TIME * 10;
    App::getInstance().addEventHandler(this);    

    return 0;
}

int CConnMgr::checkEvents() {
    int64_t curr_tick = SystemClock::uptimeMillis();
    if (curr_tick >= mNextEventTime) {
        mNextEventTime = curr_tick + TICK_TIME;
        return 1;
    }
    return 0;
}

int CConnMgr::handleEvents() {
    int64_t now_tick = SystemClock::uptimeMillis();

    if (mUartMCU) mUartMCU->onTick();

    return 1;
}
